﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Skyhook.Core;
using Microsoft.Xna.Framework;
using Skyhook.AI.PathFinder;

namespace Skyhook.AI.Animators
{
    class MovePathAnimator : Animator
    {
        MovePath pPath = null;
        Waypoint NextWaypoint = null;
        float pSpeed = 0.0f;
        
        IObjectController ObjectController = null;
        
        StraightMoveAnimator MoveAnimator = null;

        bool IsMovingToWaypoint = false;
        
        public MovePathAnimator(IObjectController objectcontroller)
            :base()
        {
            ObjectController = objectcontroller;

            MoveAnimator = new StraightMoveAnimator(ObjectController);
            MoveAnimator.OnFinish += OnWaypointReached;
        }

        public MovePath Path
        {
            get
            {
                return pPath;
            }
            set
            {
                CheckIfIsFinished();

                pPath = value;
            }
        }

        public float Speed
        {
            get
            {
                return pSpeed;
            }
            set
            {
                CheckIfIsFinished();

                pSpeed = value;
            }
        }

        protected override void PrepareAnimation()
        {
            MoveAnimator.SpeedAbsolute = pSpeed;

            pPath.StartPath();
            if (pPath.IsEmpty)
            {
                FinishAnimation();
            }

            NextWaypoint = Path.CurrentWaypoint;
        }

        protected override void DoActualAbortAnimation()
        {
            NextWaypoint = null;
        }

        protected override void DoActualUpdate(float elapsedticks)
        {
            if(NeedToStartMoving())
            {
                StartMovingToWaypoint();
                UpdateNextWaypoint();
            }

            if (IsMovingToWaypoint)
            {
                MoveAnimator.Update(elapsedticks);
            }
        }

        void StartMovingToWaypoint()
        {
            MoveAnimator.Destination = new Vector2(NextWaypoint.X + 0.5f, NextWaypoint.Y + 0.5f);
            MoveAnimator.StartAnimation();
            IsMovingToWaypoint = true;
        }

        void UpdateNextWaypoint()
        {
            if (!Path.IsLastPoint)
            {
                Path.SetNextWaypoint();
                NextWaypoint = Path.CurrentWaypoint;
            }
            else
            {
                NextWaypoint = null;
            }
        }

        bool NeedToStartMoving()
        {
            return (!IsLastWaypoint() && !IsMovingToWaypoint);
        }

        void OnWaypointReached()
        {
            IsMovingToWaypoint = false;

            if (IsLastWaypoint())
            {
                FinishAnimation();
            }
        }

        bool IsLastWaypoint()
        {
            return (NextWaypoint == null);
        }
    }
}
